# -*- coding: utf-8 -*-
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from .. import LinkExtractorRule, utils, items
import re


class AutohomeSpider(CrawlSpider):
    name = 'autohome'
    allowed_domains = ['car.autohome.com.cn', 'www.autohome.com.cn']
    start_urls = ['https://car.autohome.com.cn/diandongche/list-0-0-0-0-0-0-0-0-1.html']

    #翻页规则，
    pageLink = LinkExtractor(process_value=LinkExtractorRule.page_process_value, allow_domains=('car.autohome.com.cn'))
    #车系，
    lineLink = LinkExtractor(process_value=LinkExtractorRule.line_process_value, allow_domains=('car.autohome.com.cn'))
    #车型，
    typeLink = LinkExtractor(process_value=LinkExtractorRule.type_process_value, allow_domains=('www.autohome.com.cn'))


    #定义匹配规则，callback不可以使用parse()
    rules = (
        Rule(pageLink, follow=True),
        Rule(lineLink, callback='parse_line', follow=True),
        Rule(typeLink, callback='parse_type', follow=False),
    )

    def parse_line(self, response):
        content = response.xpath("//*[@class=\"column grid-16\"]")
        line_item = items.LineItem()

        line_item["id"] = re.compile(r"(\d+)").search(response.url).group()
        line_item["brand"] = utils.is_None(content.xpath("./div[@class=\"cartab-title\"]/h2/a/text()"))[0]
        line_item["line"] = utils.is_None(content.xpath(".//*[@class=\"list-cont-main\"]/div[@class=\"main-title\"]/a/text()"))[0]
        desc = content.xpath(".//*[@class=\"list-cont-main\"]/div[@class=\"main-lever\"]")
        if desc :
            line_item["level"] = utils.is_None(desc.xpath("./div[@class=\"main-lever-left\"]/ul/li[1]/span/text()"))[0]
            line_item["mileage"] = utils.is_None(desc.xpath("./div[@class=\"main-lever-left\"]/ul/li[2]/span/text()"))[0]
            line_item["power"] = utils.is_None(desc.xpath("./div[@class=\"main-lever-left\"]/ul/li[3]/span/text()"))[0]
            line_item["charge"] = utils.is_None(desc.xpath("./div[@class=\"main-lever-left\"]/ul/li[4]/span/text()"))[0]
            line_item["ocolor"] = utils.is_None(desc.xpath("./div[@class=\"main-lever-left\"]/ul/li[5]/div[@class=\"carcolor fn-left\"]/a/div/div/text()"))
            line_item["icolor"] = utils.is_None(desc.xpath("./div[@class=\"main-lever-left\"]/ul/li[6]/div[@class=\"carcolor fn-left\"]/a/div/div/text()"))
        yield line_item


    def parse_type(self, response):
        content = response.xpath("//*[@class=\"spec-information\"]")
        type_item = items.TypeItem()

        type_item["id"] = re.compile(r"(\d+)").search(response.url).group()
        type_item["line_id"] = utils.is_None(response.xpath("//*[@class=\"athm-sub-nav__car__name\"]/a/@href"))[0].replace("/", "")
        type_item["name"] = utils.is_None(content.xpath("./div[@class=\"information-tit\"]/h2/text()"))[0]
        desc = content.xpath("./div[@class=\"information-con\"]")
        if desc:
            type_item["price"] = re.compile(r"<(.+?)>", re.S).sub("", utils.is_None(desc.xpath(".//*[@class=\"factoryprice\"]"))[0])
            type_item["power_type"] = utils.is_None(desc.xpath("./div[@class=\"information-summary\"]/div[@class=\"spec-baseinfo\"]/ul/li[1]/span/text()"))[0]
            type_item["mileage"] = utils.is_None(desc.xpath("./div[@class=\"information-summary\"]/div[@class=\"spec-baseinfo\"]/ul/li[2]/span/text()"))[0]
            type_item["slow_charge"] = utils.is_None(desc.xpath("./div[@class=\"information-summary\"]/div[@class=\"spec-baseinfo\"]/ul/li[3]/span/text()"))[0]
            type_item["quick_charge"] = utils.is_None(desc.xpath("./div[@class=\"information-summary\"]/div[@class=\"spec-baseinfo\"]/ul/li[4]/span/text()"))[0]
            type_item["battery_capacity"] = utils.is_None(desc.xpath("./div[@class=\"information-summary\"]/div[@class=\"spec-baseinfo\"]/ul/li[5]/span/text()"))[0]
            type_item["charge_persent"] = utils.is_None(desc.xpath("./div[@class=\"information-summary\"]/div[@class=\"spec-baseinfo\"]/ul/li[6]/span/text()"))[0]
            type_item["quality_asurance"] = utils.is_None(desc.xpath("./div[@class=\"information-summary\"]/div[@class=\"spec-baseinfo\"]/ul/li[7]/span/text()"))[0]
            type_item["size"] = utils.is_None(desc.xpath("./div[@class=\"information-summary\"]/div[@class=\"spec-baseinfo\"]/ul/li[8]/span/text()"))[0]
        yield type_item
